/**
 * Created by zlq on 2017/9/30.
 */


var page = 1;
var id;


function doAjax(){
    $.ajax({
        url:'/crm/shopAdminUser/getShopUserList.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            page:page,
            shopId:$('#shopId').val(),
            userStateValue:$('#userStateValue').val(),
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.length;i++){

                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="detailsId"   >'+data.datas[i].userId+'</td></a>'+
                        '<td class="sorting_1">'+data.datas[i].userAccount+'</td>'+
                        '<td class="center ">'+data.datas[i].userRealName+'</td>'+
                        '<td class="center ">'+data.datas[i].userPhone+'</td>'+
                        '<td class="center ">'+data.datas[i].roleName+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

            }
        }
    })
}

$(function(){
    doMyShop();
    //doAjax();
})


//查询
$('#refer').click(function(){
    page=1;
    doAjax();
})


//获取我的店铺
function doMyShop(){
    $.ajax({
        url:'/crm/shopAdminUser/getMyShopList.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
        },
        type:'post',
        success:function(data){
            if(data.code==1){
                for(var i=0;i<data.datas.length;i++){
                    $('<option value="'+data.datas[i].shopId+'">'+data.datas[i].shopName+'</option>').appendTo($('#shopId'));
                    $('<option value="'+data.datas[i].shopId+'">'+data.datas[i].shopName+'</option>').appendTo($('#shopId1'));
                }
                getNotAllowRole();
            }
        }
    })
}

//获取可选角色
function getNotAllowRole(){
    $.ajax({
        url:'/crm/roleC/getNotAllowRole.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
        },
        type:'post',
        success:function(data){
            if(data.code==1){
                for(var i=0;i<data.datas.length;i++){
                    $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#role'));
                }
                doAjax();
            }
        }
    })




//点击保存
    $('#save').click(function(){

        var agencyloginName = $('#userAccount').val();
        var agencyphone = $('#userPhone').val();
        if(!checkUser(agencyloginName)){
            return;
        }
        if(!checkMobile(agencyphone)){
            return;
        }

        $.ajax({
            url:'/crm/shopAdminUser/addShopUser.json',
            dataType:'json',
            data:{
                token:localStorage.getItem('crmtoken'),
                userAccount:agencyloginName,
                realName:$('#userName').val(),
                roleId:$('#role').val(),
                phone:agencyphone,
                shopId:$('#shopId1').val(),
            },
            type:'post',
            success:function(data){
                if(data.code==0){
                    alert(data.msg)
                    return;
                }
                if(data.code==-1){
                    window.parent.location.href="/crm/login.jsp"
                }
                if(data.code==1){
                    alert('添加成功');
                    $('#myModal5').removeClass('fade in');
                    $('#myModal5').addClass('fade');
                    $('#myModal5').attr('aria-hidden',true);
                    $('#myModal5').css('display','none');
                    $('.modal-backdrop').css('display','none');
                    $('#myModal5 input').val('');
                    page=1;
                    doAjax();
                }
            }
        })
    })
}